#include "Event.h"

#include <utility>

namespace network {

    EventType operator|(EventType lhs, EventType rhs) {
        return static_cast<EventType>(static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs));
    }

    EventType operator&(EventType lhs, EventType rhs) {
        return static_cast<EventType>(static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs));
    }

    EventType& operator|=(EventType &lhs, EventType rhs) {
        lhs = lhs | rhs;
        return lhs;
    }

    EventType& operator&=(EventType &lhs, EventType rhs) {
        lhs = lhs & rhs;
        return lhs;
    }

    bool hasEvent(EventType events, EventType event) {
        return (events & event) == event;
    }

    Event::Event(int fd, Event::Handler handler, void *context) :
        fd_(fd),
        handler_(std::move(handler)),
        status_(EventStatus::Ready),
        context_(context) {}

    EventType Event::pop() {
        EventType type = queue_.top();
        queue_.pop();
        return type;
    }

    void Event::addEventLock(EventType type) {
        std::lock_guard<std::mutex> lock(mutex_);
        queue_.emplace(type);
    }

    EventType Event::popLock() {
        std::lock_guard<std::mutex> lock(mutex_);
        EventType type = queue_.top();
        queue_.pop();
        return type;
    }
}
